<!--
 * @Author: 刘十三
 * @Date: 2024-01-02 19:52:55
 * @LastEditors: 刘十三
 * @LastEditTime: 2024-03-11 17:16:32
 * @FilePath: \NM-dev-1.3.1\dev\pages\internal\linkage\todo\inspectionArrangement.vue
 * @Description: 内蒙多员联动 - 信息录入（审批）
-->

<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999"></theme-navbar>
    <!-- #endif -->
    <view class="content" v-if="show">
      <view class="header-list">
        <u-tabs :list="tabList" :show-bar="true" :is-scroll="false" :current="current" @change="handleChange" :bold="false"></u-tabs>
      </view>
      <u-divider bg-color="#f8f8f8" height="100" half-width="100%"></u-divider>
      <view class="content-main content-process" v-show="current == 0">
        <view class="no-data" v-if="form.currentHanlderCode <= 0">
          <u-icon name="warning" size="150"></u-icon>
          <view class="no-data-text">暂无流程处理</view>
        </view>
        <u-collapse :accordion="false" class="collapse-item-line" ref="collapseRef">
          <u-collapse-item index="1" :open="status == 'view' || form.currentHanlderCode == 1" title="核查安排" v-if="form.currentHanlderCode >= 1" @change="handleInitHeight">
            <informationPlan :bizUuid="bizUuid" ref="informationPlan" :is-edit="form.currentHanlderCode == 1 && status != 'view'" :form="planForm" />
          </u-collapse-item>
          <u-collapse-item index="2" :open="status == 'view' || form.currentHanlderCode == 2" title="信息核查" v-if="form.currentHanlderCode >= 2" @change="handleInitHeight">
            <informationCheck :bizUuid="bizUuid" ref="informationCheck" :is-edit="form.currentHanlderCode == 2 && status != 'view'" :form="checkForm" />
          </u-collapse-item>
          <!-- <u-collapse-item index="3" :open="status == 'view' || form.currentHanlderCode == 3" title="转办核查安排" v-if="form.currentHanlderCode >= 3" @change="handleInitHeight">
            <informationTansfer :bizUuid="bizUuid" ref="informationTansfer" :is-edit="form.currentHanlderCode == 3 && status != 'view'" :form="transForm" @heightInit="handleInitHeight" />
          </u-collapse-item> -->
          <u-collapse-item index="4" :open="status == 'view' || form.currentHanlderCode == 4" title="任务处置" v-if="form.currentHanlderCode >= 4" @change="handleInitHeight">
            <informationExecute :bizUuid="bizUuid" ref="informationExecute" :is-edit="form.currentHanlderCode == 4 && status != 'view'" :form="executeForm" />
          </u-collapse-item>
          <u-collapse-item index="5" :open="status == 'view' || form.currentHanlderCode == 5" title="任务办结" v-if="form.currentHanlderCode >= 5" @change="handleInitHeight">
            <informationCompletion :bizUuid="bizUuid" ref="informationCompletion" :is-edit="form.currentHanlderCode == 5 && status != 'view'" :form="completionForm" />
          </u-collapse-item>
        </u-collapse>
      </view>
      <!-- 基本信息 -->
      <view class="content-main" v-show="current == 1">
        <infomationBaseInfo :info="form" />
      </view>
      <!-- 附件信息 -->
      <view class="content-file" v-show="current == 2">
        <uploadFileList :doorwayFileUuid="form.doorwayFileUuid" :rlicFileUuid="form.rlicFileUuid" :otherFileUuid="form.otherFileUuid" :deletable="true" v-if="form.doorwayFileUuid && form.rlicFileUuid && form.otherFileUuid" />
      </view>
      <!-- 反馈历史 -->
      <view class="content-main" v-show="current == 3">
        <historyList :licenseNo="form.retailerLicense" v-if="form.retailerLicense" />
      </view>
      <!-- 流程进度 -->
      <view class="content-main content-flow" v-show="current == 4">
        <flow-his-no-doc :bizUuid="bizUuid" :processDefKey="processDefKey" />
      </view>
      <!-- 按钮 -->
      <view class="submit-btns btn-other-cls" v-if="status == 'edit' && current == 0 && form.currentHanlderCode > 0">
        <u-row gutter="25" style="width: 100%;overflow-x: scroll; flex-wrap: nowrap;">
          <u-col :span="btnCol">
            <u-button type="error" @click="handleBack">
              退回
            </u-button>
          </u-col>
          <u-col v-if="form.currentHanlderCode == 4" :span="btnCol">
            <u-button type="primary" @click="handleCancel">
              作废
            </u-button>
          </u-col>
          <u-col :span="btnCol">
            <u-button type="primary" @click="saveOrSubmit">
              提交
            </u-button>
          </u-col>
        </u-row>
        <view v-if="form.currentHanlderCode == 5 && form.processStatus == '04'">该流程已作废，不计入多员相关的数据统计表！</view>
      </view>
      <!-- 按钮 符合 内管部门转交 -->
      <view class="submit-btns btn-other-cls" v-if="status == 'edit' && current == 0 && form.currentHanlderCode == -2">
        <u-row gutter="25" style="width: 100%;overflow-x: scroll; flex-wrap: nowrap;">
          <u-col span="12">
            <u-button type="primary" @click="saveOrSubmit">
              提交
            </u-button>
          </u-col>
        </u-row>
      </view>
      <!-- 退回 -->
      <u-popup mode="bottom" :height="form.currentHanlderCode == 2 ? '52%' : '41%'" v-model="backShow" :safe-area-inset-bottom="true" :closeable="true">
        <view class="back-content">
          <view class="back-title" style="font-weight: bold;text-align: center;font-size:16px">退回流程</view>
          <view class="back-form" v-if="backShow">
            <u-form class="u-form-m" :model="backForm" :error-type="['border-bottom', 'toast']" label-width="auto" label-position="top" ref="bForm">
              <u-form-item label="退回节点" prop="nodeId" :required="true" v-if="form.currentHanlderCode == 2">
                <u-radio-group v-model="backForm.nodeId">
                  <u-radio v-for="(item, index) in backList" :key="index" :name="item.value">
                    {{ item.name }}
                  </u-radio>
                </u-radio-group>
              </u-form-item>
              <u-form-item label="退回意见" prop="approveMemo" :required="true">
                <view style="height: 100px;width: 100%">
                  <u-input placeholder="请输入退回意见" v-model="backForm.approveMemo" type="textarea" :auto-height="false" maxlength="500" height="90"></u-input>
                </view>
              </u-form-item>
            </u-form>
            <u-button type="primary" @click="saveBackConfirm" style="margin-top: 15px">
              提交
            </u-button>
          </view>
        </view>
      </u-popup>
    </view>
  </view>
</template>

<script>
import linkageService from "@/service/internal/linkeage.service";
import FlowHisNoDoc from "@/components/flow-his-no-doc.vue";
import historyList from "../components/historyList.vue";
import uploadFileList from "../components/uploadFileList.vue";
import infomationBaseInfo from "./components/infomationBaseInfo.vue";
import informationPlan from "./components/informationPlan.vue";
import informationCheck from "./components/informationCheck.vue";
import informationTansfer from "./components/informationTansfer.vue";
import informationExecute from "./components/informationExecute.vue";
import informationCompletion from "./components/informationCompletion.vue";

export default {
  components: { infomationBaseInfo, FlowHisNoDoc, historyList, uploadFileList, informationPlan, informationCheck, informationTansfer, informationExecute, informationCompletion },
  data() {
    return {
      processDefKey: "task-check",
      current: 0,
      tabList: [
        {
          name: "流程处理",
        },
        {
          name: "基本信息",
        },
        {
          name: "附件信息",
        },
        {
          name: "反馈历史",
        },
        {
          name: "流程进度",
        },
      ],
      backList: [
        { name: "信息录入", value: "0" },
        { name: "核查安排", value: "1" },
      ],
      show: false,
      bizUuid: "", // 待办跳转过来的参数
      status: "view", // 默认是status 请求之后修改 view：查看；edit:编辑；
      form: {}, // 初始化请求出来的form
      planForm: {},
      checkForm: {},
      transForm: {},
      executeForm: {},
      completionForm: {},
      submitForm: {}, // 要提交的表单
      isTansfer: "",
      tabIndex: 0,
      flowTotal: 0,
      backShow: false,
      backForm: {
        nodeId: "0",
        approveMemo: "",
      },
      rules: {
        nodeId: [
          {
            required: true,
            message: "请选择退回节点",
            trigger: ["blur", "change"],
          },
        ],
        approveMemo: [
          {
            required: true,
            message: "请输入退回意见",
            trigger: ["blur", "change"],
          },
        ],
      },
      abandonedFlag: 0, // 作废标志
    };
  },
  onLoad() {
    let preParam = this.getRouteParam();
    this.bizUuid = preParam.bizUuid;
    this.tabIndex = preParam.tabIndex || 0;
    this.status = preParam.status;
    this.getInitForm();
  },
  computed: {
    btnCol() {
      if (this.form.currentHanlderCode == 4) {
        return 4;
      } else {
        return 6;
      }
    }
  },
  methods: {
    handleInitHeight(param) {
      if (param.show) {
        this.$nextTick(() => {
          this.$refs.collapseRef && this.$refs.collapseRef.init();
        });
      }
    },
    getInitForm() {
      Promise.all([this.getStep(), this.getDefaultInfo()]);
    },
    handleChange(index) {
      this.current = index;
      if (index == 0) {
        this.handleInitHeight({ show: true });
      }
    },
    getStep() {
      let self = this;
      //  获取流程节点
      this.$modal.showLoading("数据加载中...");
      linkageService
        .getTaskHandlerClientList(this.bizUuid)
        .then((rs) => {
          self.$modal.hideLoading();
          if (rs.success) {
            self.show = true;
            if (rs.data.length <= 0 && this.status == "view") {
              this.tabIndex = 1;
            }
            rs.data.forEach((item) => {
              if (item.currentHanlderCode == "1") {
                self.planForm = item;
              } else if (item.currentHanlderCode == "2") {
                self.checkForm = item;
              } else if (item.currentHanlderCode == "3") {
                self.transForm = item;
              } else if (item.currentHanlderCode == "4") {
                self.executeForm = item;
              } else if (item.currentHanlderCode == "5") {
                self.completionForm = item;
              }
            });
            this.flowTotal = rs.data.length;

            setTimeout(() => {
              this.handleInitHeight({ show: true });
            }, 300);
          } else {
            self.$modal.hideLoading();
            self.$u.toast(res.msg);
          }
        })
        .catch((err) => {
          self.show = true;
          self.$modal.hideLoading();
        });
    },
    getDefaultInfo() {
      let self = this;
      // 获取详情
      this.$modal.showLoading("数据加载中...");
      linkageService
        .getTaskClientDetail(self.bizUuid)
        .then((rs) => {
          self.$modal.hideLoading();
          if (rs.success) {
            self.form = rs.bean;
            // 修改 查看任务详情bug 如果是编辑的状态 使用 详情的 currentHanlderCode 如果是查看使用流程节点的数值 fsx 2024-1-10 开始
            if (this.status != "view") {
              self.status = rs.bean.step;
              self.form.currentHanlderCode = self.form.currentHanlderCode ? Number(self.form.currentHanlderCode) : 0;
            } else {
              self.form.currentHanlderCode = this.flowTotal;
            }
            // 修改 查看任务详情bug 如果是编辑的状态 使用 详情的 currentHanlderCode 如果是查看使用流程节点的数值 fsx 2024-1-10 结束
            if (self.tabIndex) {
              let index = Number(self.tabIndex);
              self.handleChange(index);
            }
          } else {
            self.$u.toast(rs.msg);
          }
        })
        .catch((err) => {
          self.$modal.hideLoading();
        });
    },
    saveOrSubmit(isResetAbandonedFlag = true) {
      if(isResetAbandonedFlag) {
        this.abandonedFlag = 0;
      }
      if (this.form.currentHanlderCode == -2) {
        this.chooseNextHandlePersonsId();
      }
      // 核查安排提交
      if (this.form.currentHanlderCode == 1) {
        this.$refs.informationPlan.$refs.uForm.validate((valid) => {
          if (valid) {
            this.submitForm = this.$refs.informationPlan.planForm;
            this.chooseNextHandlePersonsId();
          }
        });
      }
      // 信息核查
      if (this.form.currentHanlderCode == 2) {
        this.$refs.informationCheck.$refs.uForm.validate((valid) => {
          if (valid) {
            this.submitForm = this.$refs.informationCheck.checkForm;
            this.chooseNextHandlePersonsId();
          }
        });
      }
      // 信息核查
      if (this.form.currentHanlderCode == 3) {
        this.$refs.informationTansfer.$refs.uForm.validate((valid) => {
          if (valid) {
            this.submitForm = this.$refs.informationTansfer.transForm;
            this.isTansfer = this.submitForm.transfer;
            // 这里修改为信息核查结果为不一致 && 转办节点选择否
            if (this.checkForm.checkMatchStatus == 1 && this.submitForm.transfer == "0") {
              this.handleConfirm();
            } else {
              this.chooseNextHandlePersonsId();
            }
          }
        });
      }
      // 任务处置
      if (this.form.currentHanlderCode == 4) {
        this.$refs.informationExecute.$refs.uForm.validate((valid) => {
          if (valid) {
            this.submitForm = this.$refs.informationExecute.executeForm;
            this.chooseNextHandlePersonsId();
          }
        });
      }

      // 任务办结
      if (this.form.currentHanlderCode == 5) {
        this.$refs.informationCompletion.$refs.uForm.validate((valid) => {
          if (valid) {
            this.submitForm = this.$refs.informationCompletion.completionForm;
            this.handleConfirm();
          }
        });
      }
    },
    // 提交代码
    handleConfirm(nextHandlePersonsId) {
      if (nextHandlePersonsId) {
        this.submitForm.nextHandlePersonsId = nextHandlePersonsId;
      }
      this.submitForm.taskId = this.bizUuid;
      let param = Object.assign({}, this.submitForm);

      if (this.form.currentHanlderCode == 1) {
        param.plannStatus = this.submitForm.plannStatus ? Number(this.submitForm.plannStatus) : this.submitForm.plannStatus;
      }
      if (this.form.currentHanlderCode == 2) {
        param.checkMatchStatus = this.submitForm.checkMatchStatus ? Number(this.submitForm.checkMatchStatus) : this.submitForm.checkMatchStatus;
      }
      if (this.form.currentHanlderCode == 3) {
        param.transfer = this.submitForm.transfer ? Number(this.submitForm.transfer) : this.submitForm.transfer;
        param.transferPlaned = this.submitForm.transferPlaned ? Number(this.submitForm.transferPlaned) : this.submitForm.transferPlaned;
      }
      if (this.form.currentHanlderCode == 4) {
        param.executed = this.submitForm.executed ? Number(this.submitForm.executed) : this.submitForm.executed;
        if(this.abandonedFlag == 1) {
          param.abandonedFlag = 1;
        }
      }
      if (this.form.currentHanlderCode == 5) {
        param.executed = this.submitForm.executed ? Number(this.submitForm.executed) : this.submitForm.executed;
      }
      let self = this;
      self.$modal.showLoading("数据提交中...");
      linkageService
        .saveAndSubmit(param)
        .then((res) => {
          self.$modal.hideLoading();
          if (res.success) {
            self.$u.toast("提交成功");
            self.backPrev({
              paramKey: "refresh",
              param: true,
            });
          } else {
            self.$u.toast(res.msg);
          }
        })
        .catch((err) => {
          self.$modal.hideLoading();
        });
    },
    // 选择下一节审批人
    chooseNextHandlePersonsId() {
      let self = this;
      self.$u.route({
        url: "/pages/internal/linkage/components/flowUserList",
        params: {
          status: "3",
          index: self.form.currentHanlderCode,
          gfCode: "task-check",
          bizId: self.bizUuid,
          isTansfer: self.isTansfer,
        },
      });
    },
    handleBack() {
      this.backShow = true;
      this.$nextTick(() => {
        this.$refs.bForm.setRules(this.rules);
      });
    },
    saveBackConfirm() {
      let self = this;
      self.$refs.bForm.validate((valid) => {
        if (valid) {
          if (self.form.currentHanlderCode == 4) {
            self.backForm.nodeId = "2";
          }
          if (self.form.currentHanlderCode > 4) {
            self.backForm.nodeId = "1";
          }
          let param = {
            nodeId: self.backForm.nodeId,
            bizId: self.bizUuid,
            approveMemo: self.backForm.approveMemo,
            processDefKey: self.processDefKey,
            prtFrist: 1,
          };
          self.$modal.showLoading("数据加载中...");
          linkageService
            .setPrevFistStep(param)
            .then((rs) => {
              self.$modal.hideLoading();
              if (rs.success) {
                self.$u.toast("流程已退回");
                self.backPrev({
                  paramKey: "refresh",
                  param: true,
                });
              } else {
                self.$u.toast(rs.msg);
              }
            })
            .catch((err) => {
              self.$modal.hideLoading();
            });
        }
      });
    },
    handleFindLicoStatus() {
      linkageService.findByLicNo({ licNo: this.form.retailerLicense }).then((rs) => {
        if (rs.success) {
          if (rs.bean) {
            this.$modal.alert(`零售户当前状态为：${rs.bean.licStatusName}`);
          } else {
            this.$modal.alert("当前零售户不存在！");
          }
        } else {
          this.$message.error(rs.message);
        }
      });
    },
    updateBackState(param) {
      if (param.flowUserSelectBack) {
        this.handleConfirm(param.flowUserSelectBack);
      }
    },
    handleCancel() {
      this.$modal.confirm('作废后，该条业务数据将不计入多员相关的数据统计表',() => {
        this.abandonedFlag = 1;
        this.saveOrSubmit(false)
      }, () => {
        this.abandonedFlag = 0;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  background-color: #f8f8f8;
  padding: 0;
  height: auto;
  overflow: hidden;
  min-height: 100%;
}
.content {
  .header-list {
    position: fixed;
    background: white;
    padding: 0rpx 24rpx;
    /* #ifdef H5-ONLY  */

    top: 44px;
    /* #endif */
    /* #ifndef H5-ONLY  */
    top: 0px;
    /* #endif */
    left: 0px;
    width: 100%;
    z-index: 999;
    box-sizing: border-box;
  }
  .content-main {
    padding: 0rpx 30rpx 0rpx;
    margin-bottom: 100px;
  }
  .content-process {
    padding: 0;
  }
  .content-flow {
    background: white;
    margin: 0rpx 30rpx 100px;
    border-radius: 15px;
    padding: 30rpx 40rpx 30rpx;
  }
  /deep/ .collapse-item-line {
    position: relative;
    .u-collapse-item {
      background: white;
      padding: 0rpx 30rpx 20rpx;
      margin-bottom: 10px;
    }
    .u-collapse-head::before {
      content: "";
      width: 4px;
      height: 16px;
      border-radius: 10px;
      background: #2979ff;
    }
    .u-collapse-title {
      padding-left: 5px;
    }
    .u-collapse-content {
      padding: 0px 5px;
    }
  }
  .no-data {
    padding-top: 100px;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .back-content {
    padding: 30rpx 50rpx;
    height: 100%;
    overflow: hidden;
  }
  /deep/ .back-form {
    .u-input__textarea {
      height: 100px;
    }
  }
}
</style>
